<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>倒数计数器，到 0 停止</title>
</head>
<body>
<!--使用原生 js 编写一个计数器，初始值为10， 每秒减一，到0 自动停止-->
<!--添加一个按钮，点击按钮后暂停-->
<!--再次点击后,继续计数-->
<div id="counter">

</div>

<div><button id="btn">暂停</button></div>
<script>
    let element = document.getElementById("counter");
    let button = document.getElementById("btn");
    let count = 10

    let status = true
    let interval = null
    let timeout = null

    // 设置显示的初始值
    element.innerText = count.toString()

    interval = setInterval(function () {
        element.innerText = count.toString()
        count --
    }, 1000);

    timeout = setTimeout(function () {
        clearInterval(interval)
    }, 11000);

    button.onclick = function () {
        if (status) {
            clearInterval(interval)
            clearInterval(timeout)
            status = !status
            button.innerText= "继续"

        }
        else {
            interval = setInterval(function () {
                element.innerText = count.toString()
                count --
            }, 1000);

            timeout = setTimeout(function () {
                clearInterval(interval)
            }, 11000-(10-count)*1000);

            status = !status
            button.innerText= "暂停"
        }

    }

</script>
</body>
</html>